home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AOL File Library: 2,801 to 2,900
/
aol-file-protocol-4400-2801-to-2900.zip
/
AOLDLs
/
C++ Files Library
/
Binary iostream class
/
bstream++.sit
/
bstream++
/
bstream.cp
next >
Wrap
Text File
|
1993-08-11
|
4KB
|
160 lines
// bstream.cp -- Implementation of bstream class.
//
// These are the insterters and extractors for the built in types.
//
// ComputKit
// Kitzel Hoover (KitzelH@aol.com)
// August 9, 1993
//
// You are free to use, copy, and modify this code. However, please
// report any bugs to me.
//
#include <fstream.h>
#include "bstream.h"
int strlen (const char* string) {
int count = 0;
while (*string++) count++;
return count;
}
// Inserters ------------------------------------------------------
bstream & bstream::operator<<(const char *string) {
unsigned char len = strlen(string);
if (string_width && (string_width < len))
len = string_width; // truncate string
fstream::write(&len,sizeof(len)); // write length byte
fstream::write(string,len); // write string
if (string_width && (string_width > len))
for (int i = string_width - len; i > 0; i--)
fstream::put('\0'); // pad if needed
if (string_width) string_width = 0; // reset to default
return *this;
};
bstream &bstream::operator<<(char c) {
fstream::put(c);
return *this;
};
bstream &bstream::operator<<(short v) {
fstream::write(&v,sizeof(v));
return *this;
}
bstream &bstream::operator<<(int v) {
fstream::write(&v,sizeof(v));
return *this;
}
bstream &bstream::operator<<(long v) {
fstream::write(&v,sizeof(v));
return *this;
}
bstream &bstream::operator<<(unsigned short v) {
fstream::write(&v,sizeof(v));
return *this;
}
bstream &bstream::operator<<(unsigned v) {
fstream::write(&v,sizeof(v));
return *this;
}
bstream &bstream::operator<<(unsigned long v) {
fstream::write(&v,sizeof(v));
return *this;
}
bstream &bstream::operator<<(float v) {
fstream::write(&v,sizeof(v));
return *this;
}
bstream &bstream::operator<<(double v) {
fstream::write(&v,sizeof(v));
return *this;
}
bstream &bstream::operator<<(long double v) {
fstream::write(&v,sizeof(v));
return *this;
}
// Extractors -------------------------------------------------------
bstream &bstream::operator>>(char *string) {
unsigned char len, trash;
fstream::read(&len, sizeof(len));
string[0] = '\0'; // start with empty string
fstream::get(string,len+1,'\0'); // leaves null on stream
string[len] = 0;
if (string_width && (string_width>len))
for (int i = string_width - len; i>0; i--) {
fstream::get(trash); // skip padding
}
if (string_width) string_width = 0; // reset to default
return *this;
}
bstream &bstream::operator>>(char &c) {
fstream::get(c);
return *this;
}
bstream &bstream::operator>>(short &v) {
fstream::read (&v, sizeof(v) );
return *this;
}
bstream &bstream::operator>>(int &v) {
fstream::read (&v, sizeof(v) );
return *this;
}
bstream &bstream::operator>>(long &v) {
fstream::read (&v, sizeof(v) );
return *this;
}
bstream &bstream::operator>>(unsigned short &v) {
fstream::read (&v, sizeof(v) );
return *this;
}
bstream &bstream::operator>>(unsigned int &v) {
fstream::read (&v, sizeof(v) );
return *this;
}
bstream &bstream::operator>>(unsigned long &v) {
fstream::read (&v, sizeof(v) );
return *this;
}
bstream &bstream::operator>>(float &v) {
fstream::read (&v, sizeof(v) );
return *this;
}
bstream &bstream::operator>>(double &v) {
fstream::read (&v, sizeof(v) );
return *this;
}
bstream &bstream::operator>>(long double &v) {
fstream::read (&v, sizeof(v) );
return *this;
}
// Manipulators ----------------------------------------------------
bstream& operator<< (bstream& bs, swidth& sw) {
bs.setwidth(sw.val);
return bs;
}
bstream& operator>> (bstream& bs, swidth& sw) {
bs.setwidth(sw.val);
return bs;
}